package com.ml.mall.service.sys.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.sys.SysSubRoutingNewDto;
import com.ml.mall.bean.req.sys.SysSubscriptionEditDto;
import com.ml.mall.bean.req.sys.SysSubscriptionNewDto;
import com.ml.mall.bean.req.sys.SysSubscriptionQueryPageDto;
import com.ml.mall.entity.sys.SysSubscription;
import com.ml.mall.service.sys.SysSubscriptionRoutingService;
import com.ml.mall.service.sys.SysSubscriptionService;
import com.ml.mall.mapper.SysSubscriptionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 */
@Service
public class SysSubscriptionServiceImpl extends ServiceImpl<SysSubscriptionMapper, SysSubscription>
    implements SysSubscriptionService{

    @Autowired
    private SysSubscriptionMapper sysSubscriptionMapper;

    @Autowired
    private SysSubscriptionRoutingService subscriptionRoutingService;

    /**
     * 新建配套
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Integer subscriptionNew(SysSubscriptionNewDto dto) {
        //添加配套信息
        SysSubscription sysSubscription = new SysSubscription();
        BeanUtils.copyProperties(dto, sysSubscription);
        int result = sysSubscriptionMapper.insert(sysSubscription);

        //添加配套路由信息
        SysSubRoutingNewDto sysSubRoutingNewDto = new SysSubRoutingNewDto();
        sysSubRoutingNewDto.setSubId(sysSubscription.getId());
        BeanUtil.copyProperties(dto, sysSubRoutingNewDto);
        subscriptionRoutingService.subRoutingNew(sysSubRoutingNewDto);

        return result;
    }

    /**
     * 分页查询配套
     * @param dto
     * @return
     */
    @Override
    public IPage<SysSubscription> subscriptionQueryPage(SysSubscriptionQueryPageDto dto) {
        IPage<SysSubscription> subscriptionIPage = new Page<>(dto.getPageIndex(), dto.getPageNum());
        LambdaQueryWrapper<SysSubscription> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getRoleType() != null, SysSubscription::getRoleType, dto.getRoleType())
                    .like(dto.getName() != null, SysSubscription::getSubName, dto.getName());

        return sysSubscriptionMapper.selectPage(subscriptionIPage, queryWrapper);
    }

    /**
     * 编辑配套
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Integer subscriptionEdit(SysSubscriptionEditDto dto) {
        //修改配套数据
        SysSubscription subscription = new SysSubscription();
        BeanUtil.copyProperties(dto, subscription);
        int result = sysSubscriptionMapper.updateById(subscription);

        //修改配套路由
        if(0 != result){
            SysSubRoutingNewDto sysSubRoutingNewDto = new SysSubRoutingNewDto();
            sysSubRoutingNewDto.setSubId(subscription.getId());
            BeanUtil.copyProperties(dto, sysSubRoutingNewDto);
            subscriptionRoutingService.subRoutingNew(sysSubRoutingNewDto);
        }
        return result;
    }

    @Override
    public Long queryInitialSubscriptionId(Integer roleType) {
        return sysSubscriptionMapper.selectMinIdByRoleType(roleType);
    }
}




